package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfCliffordDAO;
import com.ljkj.common.db.dao.ZjfCliffordRecordDAO;
import com.ljkj.common.db.dao.ZjfUserDAO;
import com.ljkj.common.db.domain.ZjfClifford;
import com.ljkj.common.db.domain.ZjfCliffordRecord;
import com.ljkj.common.db.domain.ZjfPayment;
import com.ljkj.common.db.domain.ZjfUser;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.zjf.common.RandomCode;

public class ZjfCliffordService {

	private ZjfCliffordDAO zjfCliffordDAO;
	private TLogService tLogService;
	private ZjfUserDAO zjfUserDAO;
	private ZjfCliffordRecordDAO zjfCliffordRecordDAO;


	public void setZjfCliffordRecordDAO(ZjfCliffordRecordDAO zjfCliffordRecordDAO) {
		this.zjfCliffordRecordDAO = zjfCliffordRecordDAO;
	}


	/**
	 * 获取灵犀记录
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getCliffordsByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){

		Class[] pojos = {ZjfClifford.class,ZjfUser.class};
		String[] propertyNames={"zjfClifford.user_webchat_openID"};
		Object[] values={"zjfUser.user_webchat_openID"};
		String[] likePropertyNames = {"zjfUser.userName"};
		Object[] likevalues={params};
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;

		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfCliffordDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfCliffordDAO.setPageMaxCount(pageSize);
		List<Object[]> list = this.zjfCliffordDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);

		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(Object object[] : list){
			ZjfClifford ZjfClifford=(ZjfClifford)object[0];
			ZjfUser zjfUser=(ZjfUser)object[1];
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("clifford_Id",ZjfClifford.getClifford_Id());
			entry_map.put("goodsName",ZjfClifford.getGoodsName());
			entry_map.put("goodsPrice",ZjfClifford.getGoodsPrice());
			entry_map.put("userName",zjfUser.getUserName());
			entry_map.put("userMobile",zjfUser.getUserMobile());
			entry_map.put("cliffordStartedTime",ZjfClifford.getCliffordStartedTime());
			entry_map.put("cliffordState",ZjfClifford.getCliffordState());
			entry_map.put("disabledState",ZjfClifford.getDisabledState());

			entry_list.add(entry_map);
		}

		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);

		return JSON.toJSONString(result_map);
	}
	
	
	/**
	 * 获取灵犀记录
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getCliffordRecordsByProperties(String params,String clifford_Id,int currentPage,int pageSize,String sortField,String sortOrder){

		Class[] pojos = {ZjfCliffordRecord.class};
		String[] propertyNames= {"cliffordRecordId"};
		Object[] values= {clifford_Id};
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;

		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfCliffordRecordDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfCliffordRecordDAO.setPageMaxCount(pageSize);
		List<ZjfCliffordRecord> list = this.zjfCliffordRecordDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);

		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfCliffordRecord zjfCliffordRecord : list){
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("id",zjfCliffordRecord.getId());
			if(zjfCliffordRecord.getUser_webchat_openID() != null && !"".equals(zjfCliffordRecord.getUser_webchat_openID())){
				List<ZjfUser> zjfUsers = this.zjfUserDAO.findByProperty("user_webchat_openID", zjfCliffordRecord.getUser_webchat_openID());
				if(zjfUsers.size()> 0 ){
					entry_map.put("userNickName",zjfUsers.get(0).getUserName());
				}
			}
			entry_map.put("clifford_record_money",zjfCliffordRecord.getClifford_record_money());
			entry_map.put("cliffordRecordTime",zjfCliffordRecord.getCliffordRecordTime());
			entry_map.put("order_num",zjfCliffordRecord.getOrder_num());
			entry_map.put("is_system",zjfCliffordRecord.getIs_system());

			entry_list.add(entry_map);
		}

		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);

		return JSON.toJSONString(result_map);
	}


	/**
	 * 新增或更新退款记录
	 * @param 
	 * @return
	 */
		public String editCliffordInfo(ZjfClifford zjfClifford,Tuser tuser){
			
			Tlog tlog = new Tlog();
			
			Map<String,String> result_map = new HashMap<String, String>();
			if(zjfClifford.getClifford_Id() == null){
				result_map.put("err_msg", "兑换失败，没有祈福记录。");
				result_map.put("err_code", "1");
			}else{
				try{
					
					if(zjfClifford.getDisabledState()!=null && zjfClifford.getDisabledState() == 1){
						tlog.setOpType(0);
						tlog.setContent("祈福记录状态："+zjfClifford.getClifford_Id()+" 变为不可用");
					}else{
						tlog.setOpType(1);
						tlog.setContent("修改祈福记录："+zjfClifford.getClifford_Id());
					}
					
					zjfClifford.setUpdateTime(DateTime.getDateTime());
					zjfClifford.setUpdateUser(tuser.getLoginName());
					this.zjfCliffordDAO.update(zjfClifford);
					this.tLogService.addOrEditTlogInfo(tlog, tuser);
					
					result_map.put("err_msg", "更新成功。");
					result_map.put("err_code", "0");
				}catch(Exception ex){
					ex.printStackTrace();
					result_map.put("err_msg", "更新失败。");
					result_map.put("err_code", "1");
				}
			}
			
			return JSON.toJSONString(result_map);
		}
		public ZjfClifford getCliffordById(String clifford_Id){
			return this.zjfCliffordDAO.findById(clifford_Id);
		}

	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}



	public void setZjfCliffordDAO(ZjfCliffordDAO zjfCliffordDAO) {
		this.zjfCliffordDAO = zjfCliffordDAO;
	}


	public void setZjfUserDAO(ZjfUserDAO zjfUserDAO) {
		this.zjfUserDAO = zjfUserDAO;
	}

}
